package com.example.service.impl.report;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.hz.MOCTA;
import com.example.entity.report.PengCheng.LineChartJM;
import com.example.entity.report.PengCheng.LineChartWorkCenterJM;
import com.example.entity.report.PengCheng.MoctyJM;
import com.example.entity.report.PengCheng.TopJM;
import com.example.entity.yifei.Coptd;
import com.example.entity.yifei.Purtd;
import com.example.mapper.CoptdMapper;
import com.example.mapper.MoctaMapper;
import com.example.mapper.MoctyMapper;
import com.example.mapper.PurtdMapper;
import com.example.mapper.report.PengChengReportMapper;
import com.example.service.report.PengChengReportService;
import com.example.untils.StringUtils;
import com.example.untils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PengChengReportServiceImpl implements PengChengReportService {

    @Autowired
    private PengChengReportMapper mapper;

    @Autowired
    private MoctyMapper moctyMapper;

    @Autowired
    private MoctaMapper moctaMapper;

    @Autowired
    private CoptdMapper coptdMapper;

    @Autowired
    private PurtdMapper purtdMapper;

    @Override
    public TopJM topJM() {
        // SELECT COUNT(*) FROM COPTD WHERE TD021 = 'Y' AND TD013 < CONVERT(CHAR(8),GETDATE(),112) AND TD016 = 'N'
        TopJM res = new TopJM();
        res.setOverDueSaleOrderNums(coptdMapper.selectCount(new LambdaQueryWrapper<Coptd>().eq(Coptd::getTd021, "Y").eq(Coptd::getTd016, "N").lt(Coptd::getTd013, TimeUtils.HZStdTime())));
        res.setOverDueWorkOrderNums(moctaMapper.selectCount(new LambdaQueryWrapper<MOCTA>().eq(MOCTA::getTA013, "Y").ne(MOCTA::getTA011, "Y").ne(MOCTA::getTA011, "y").lt(MOCTA::getTA010, TimeUtils.HZStdTime())));
        res.setOverDuePurchaseOrderNums(purtdMapper.selectCount(new LambdaQueryWrapper<Purtd>().eq(Purtd::getTd018, "Y").eq(Purtd::getTd016, "N").lt(Purtd::getTd012, TimeUtils.HZStdTime())));
        return res;
    }

    @Override
    public List<MoctyJM> moctyJM() {
        List<MoctyJM> res = mapper.MoctyJM();
        for(MoctyJM t : res) {
            if(StringUtils.isNotEmpty(t.getIsFinish())) {
                if("Y".equals(t.getIsFinish().trim()) || "y".equals(t.getIsFinish().trim())) {
                    t.setIsFinish("完成");
                }else{
                    t.setIsFinish("未完成");
                }
            }
        }
        return res;
    }

    @Override
    public LineChartJM lineChartJM() {
        LineChartJM res = new LineChartJM();
        List<LineChartWorkCenterJM> list = mapper.lineChartJM();
        Map<String, String> map = new HashMap<>();
        for(LineChartWorkCenterJM item : list) {
            map.put(item.getWorkCenter()+item.getDates(), "1");
        }
        List<String> workCenterList = mapper.workCenterList();
        String time = TimeUtils.HZStdTime();
        for(String workCenter : workCenterList) {
            for(int cnt = 0; cnt < 7; cnt++) {
                String date = TimeUtils.addOrSubtract(Integer.parseInt(time.substring(0, 4)), Integer.parseInt(time.substring(4, 6)),
                        Integer.parseInt(time.substring(6, 8)), -cnt);
//                String date = TimeUtils.addOrSubtract(2021, 4, 17, -cnt);
//                System.out.println("date : " + date);
                String s = map.get(workCenter + date);
                if(s == null) {
                    list.add(new LineChartWorkCenterJM(date, workCenter, new BigDecimal(0D)));
                }
            }
        }
        list.sort(new Comparator<LineChartWorkCenterJM>() {
            @Override
            public int compare(LineChartWorkCenterJM o1, LineChartWorkCenterJM o2) {
                return o1.getDates().compareTo(o2.getDates());
            }
        });
        res.setList(list);
        res.setWorkCenterList(workCenterList);
        return res;
    }
}
